package com.ruoyi.commodity.controller;

import com.ruoyi.commodity.domain.Commodity;
import com.ruoyi.commodity.domain.CommodityCategory;
import com.ruoyi.commodity.domain.vo.CommodityTreeSelect;
import com.ruoyi.commodity.service.ICommodityCategoryService;
import com.ruoyi.commodity.service.ICommodityService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: Steven
 * @date: 2023/7/7 0007 10:52
 **/
@RestController
@RequestMapping("/commodity")
public class CommodityController extends BaseController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private ICommodityCategoryService commodityCategoryService;
    /**
     * @description:新增商品
     * @author: Steven
     * @date: 2023/7/9 0009 9:17
     * @param: [commodity]
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Commodity commodity)
    {
        if (!commodityService.checkCommodityNameUnique(commodity))
        {
            return error("新增商品'" + commodity.getName() + "'失败，商品已存在");
        }
        commodity.setCreateBy(SecurityUtils.getUsername());
        return toAjax(commodityService.insertCommodity(commodity));
    }


    /**
     * @description:分页查询商品列表
     * @author: Steven
     * @date: 2023/7/9 0009 9:18
     * @param: [commodity]
     * @return: com.ruoyi.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    public TableDataInfo list(Commodity commodity)
    {
        startPage();
        List<Commodity> list = commodityService.serviceSelectCommodityList(commodity);
        return getDataTable(list);
    }


    /**
     * @description:导出商品信息
     * @author: Steven
     * @date: 2023/7/9 0009 9:18
     * @param: [response, commodity]
     * @return: void(excel)
     **/
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Commodity commodity)
    {
        List<Commodity> list = commodityService.serviceSelectCommodityList(commodity);
        ExcelUtil<Commodity> util = new ExcelUtil<>(Commodity.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * @description:根据商品ID查询商品具体信息
     * @author: Steven
     * @date: 2023/7/9 0009 9:19
     * @param: [commodityId]
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @GetMapping(value = {"/{commodityId}" })
    public AjaxResult getInfo(@PathVariable Long commodityId)
    {
        return success(commodityService.selectCommodityById(commodityId));
    }

    /**
     * @description:更新商品信息
     * @author: Steven
     * @date: 2023/7/11 0011 15:56
     * @param: [commodity]
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @PutMapping
    @Log(title = "角色管理",businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated @RequestBody Commodity commodity){
        if(!commodityService.checkCommodityNameUnique(commodity)){
            return error("修改商品"+commodity.getCommodityName()+"失败，商品名称已存在");
        }
        else{
            commodity.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(commodityService.updateCommodity(commodity));
        }
    }

    /**
     * @description:删除商品信息
     * @author: Steven
     * @date: 2023/7/16 0016 19:26
     * @param: [commodityIds]
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @DeleteMapping(value = "/{commodityIds}")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] commodityIds){
        return toAjax(commodityService.deleteCommodityById(commodityIds));
    }

    /**
     * @description:获取部门列表
     * @author: Steven
     * @date: 2023/7/16 0016 19:32
     * @param: [commodityCategory]
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/categoryTree")
    public AjaxResult categoryTree(CommodityCategory category)
    {
        return success(commodityCategoryService.selectCategoryTreeList(category));
    }


}
